﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using Dln.Common;
using Dln.Common.Helper;
using Dln.Handler;

namespace Dln.Business
{
    public class WinXinShareBusiness
    {
        public class GetTicket
        {
            public string errcode { get; set; }
            public string errmsg { get; set; }
            public string ticket { get; set; }
            public string expires_in { get; set; }
        }

        public static Hashtable GetShare(string url)
        {
            try
            {
                string AppID = CommonHelper.GetAppSettings("AppID");
                string appid = AppID;
                string noncestr = RandString();
                string time = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString();

                var ticket = HttpContext.Current.Session["getticket"];
                if (ticket == null || ticket=="")
                {
                    string newjson = RequestHelper.SendGet(String.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", WeiXinConfig.AccessToken));
                    GetTicket getticket = null;
                    if (!String.IsNullOrEmpty(newjson))
                    {
                        try
                        {
                            getticket = JSONHelper.FromJson<GetTicket>(newjson);
                        }
                        catch (Exception ex)
                        {

                        }
                    }
                    ticket = getticket.ticket;
                    HttpContext.Current.Session["getticket"] = ticket;
                    HttpContext.Current.Session.Timeout = 20;
                }
                string string1 = string.Format("jsapi_ticket={0}&noncestr={1}&timestamp={2}&url={3}", ticket, noncestr, time, url);
                string resp = SHA1(string1);
                Hashtable ht = new Hashtable();
                ht["appid"] = appid;
                ht["time"] = time;
                ht["noncestr"] = noncestr;
                ht["resp"] = resp;
                return ht;
            }
            catch (Exception ex)
            {
                Hashtable ht = new Hashtable();
                ht["appid"] = "";
                ht["time"] = "";
                ht["noncestr"] = "";
                ht["resp"] = "";
                return ht;
            }
        }

        protected static string RandString()
        {
            string str = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+";//75个字符
            Random r = new Random();
            string result = string.Empty;

            //生成一个8位长的随机字符，具体长度可以自己更改
            for (int i = 0; i < 16; i++)
            {
                int m = r.Next(0, 75);//这里下界是0，随机数可以取到，上界应该是75，因为随机数取不到上界，也就是最大74，符合我们的题意
                string s = str.Substring(m, 1);
                result += s;
            }

            return result;
        }

        private static string SHA1(string text)
        {
            byte[] cleanBytes = Encoding.Default.GetBytes(text);
            byte[] hashedBytes = System.Security.Cryptography.SHA1.Create().ComputeHash(cleanBytes);
            return BitConverter.ToString(hashedBytes).Replace("-", "");
        }
    }
}
